package com.yc.shadowdemo.demo

import android.os.Bundle
import android.util.Log
import android.widget.Button
import androidx.appcompat.app.AppCompatActivity
import androidx.lifecycle.DefaultLifecycleObserver
import androidx.lifecycle.LifecycleOwner
import com.yc.commontlib.base.YcBaseActivity
import com.yc.commontlib.extension.checkNet
import com.yc.commontlib.extension.ycLogE
import com.yc.commontlib.net.YcResult
import com.yc.commontlib.net.YcRetrofitUtil
import com.yc.commontlib.net.doFail
import com.yc.commontlib.net.doSuccess
import com.yc.shadowdemo.R
import com.yc.shadowdemo.databinding.TestNetActivityBinding
import kotlinx.coroutines.GlobalScope
import kotlinx.coroutines.flow.collect
import kotlinx.coroutines.flow.flow
import kotlinx.coroutines.launch

/**
 * Creator: yc
 * Date: 2021/6/3 15:15
 * UseDes:
 */
class TestNetActivity : YcBaseActivity<TestNetActivityBinding>(TestNetActivityBinding::inflate) {
    protected val apiService: ApiService by lazy { YcRetrofitUtil.Instance.getApiService(ApiService::class.java) }
    override fun initView() {
        setContentView(R.layout.test_net_activity)
        findViewById<Button>(R.id.btnTestNet).setOnClickListener {
            getVersionData()
        }
    }

    private fun getVersionData() {
        GlobalScope.launch {
            flow {
                val json = apiService.getVersion()
                emit(YcResult.Success((json)))
            }.checkNet().collect {
                it.doSuccess {
                    ycLogE(it)
                }
                it.doFail {
                    ycLogE(it.msg)
                }
            }
        }
    }

}